package com.block.datapush.form.item.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;

public class YiXinHuaUtil {

    public static void main(String[] args) throws Exception {
        String secretKey = "af4bd50a21fe0851a24c7bf3";
        JSONObject dataJson = new JSONObject();
        dataJson.put("idno_md5", "9b5eaa1f623ed663c9e6969e2b1ed04f");
        dataJson.put("phone_md5", "75a8e965cfd8396de1d8636484d5835f");
        dataJson.put("city", "浙江省/杭州市");
        String dataStr = encrypt(dataJson.toJSONString(), secretKey);

        JSONObject reqJson = new JSONObject();
        dataStr = "6fX+i9jvjiOYeNpy7cS65tJU4LN0hg/KGJqq4sQb9gbLYWyoNCkS5c4t+l+Utcg1ny2tGLh3Z+GBji8+ZT8isLFMpE0YrbDiaLq/eoHFDWG2ZD3k5A8dyIkFDxY9PfX+LwqZi1jt6J0Jt8VUZIEDmoErt4pcORzenw8dfnKUF2BkhYyJKgMRHq4b2LuS0oeALBMWiVCj+yaRBvgWSOqCBlJ1AWm8wy2oVVX7ld/SO0gWC7zwJd2hKcmhZZghl1EbExYGqhz+IvFO5gLUCnhZIXj38n4nJKFJCJe7MqdT/RolhoLTr7mIP5KP+tPOhiUBghQrZFy6xifZIlADd0snltE4rRMd5YYsMNg0Bfwl+Nq1nrRw99RG+ZdOWYJDOl8Ul/rIGtGWENVPNAIHC1WDhSy4RehqE3zY5O3VSAcREJIVtJjVw00H9n8HmzwUjWH5mh08NeKpghW4Y2ANQV3QpzcXjHv66eRIy4wiqCG8AggAZUf00khxb+sUzH1G97ZDjTTbfINHvfdB13xE9mi/UBR9vJHD6KMxEE+JLk+iiQx/c6Ifuuw/+/Yn7giHrSg36o8Qo7m+EBfy7NaR9QP0vFpuTzR9DQenOVMukpXGiYfcbq3HPOfG8fqE98pgKzT9Th7V+1BXgKmHPFSwvZ7VL4JAZX5r5ZxQXtLkxLWP3tt46NmEv7W3R1/zcbLRQ+HHrWhqSsNY0Ngiq7d8+/pYfdIHCcEDHqhnldArIYrtwRQ=";
        reqJson.put("data", dataStr);
        System.out.println("请求参数：" + reqJson);
        System.out.println("解密开始：");
        String decryptStr = decrypt(reqJson.getString("data"), secretKey);
        System.out.println("解密结果：" + decryptStr);
    }

    public static String encrypt(String data, String secretKey) throws Exception {
        byte[] encrpyted = tripleDES(Cipher.ENCRYPT_MODE, data.getBytes(StandardCharsets.UTF_8), secretKey.getBytes());
        return new String(Base64.encodeBase64(encrpyted));
    }

    public static String decrypt(String data, String secretKey) throws Exception {
        byte[] decoded = Base64.decodeBase64(data); // Base64 解码
        byte[] decrypted = tripleDES(Cipher.DECRYPT_MODE, decoded, secretKey.getBytes());// 3DES 解密
        return new String(decrypted, StandardCharsets.UTF_8);
    }

    private static byte[] tripleDES(int opmode, byte[] data, byte[] secretKey) throws Exception {
        return cipher("DESede", "DESede/CBC/PKCS5Padding", opmode, data, "01234567".getBytes(), secretKey);
    }

    private static byte[] cipher(String algorithm, String transformation, int opmode, byte[] data, byte[] iv,
                                 byte[] secretKey) throws Exception {
        Key key = SecretKeyFactory.getInstance(algorithm).generateSecret(new DESedeKeySpec(secretKey));
        IvParameterSpec spec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance(transformation);
        cipher.init(opmode, key, spec);
        return cipher.doFinal(data);
    }

    public static String getZhima(String zhimaScore) {
        if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm001.getValue(), zhimaScore)){
            return "0";
        }else if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm002.getValue(), zhimaScore)){
            return "350";
        }else if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm003.getValue(), zhimaScore)){
            return "630";
        }else if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm004.getValue(), zhimaScore)){
            return "680";
        }else if (StrUtil.equals(BaseTypeInfoEnum.ZhimaScore.zm005.getValue(), zhimaScore)){
            return "750";
        }
        return "0";
    }

    public static String getCar(String car) {
        if (StrUtil.equals(BaseTypeInfoEnum.Car.car002.getValue(), car)){
            return "有车（全款）";
        }else if (StrUtil.equals(BaseTypeInfoEnum.Car.car001.getValue(), car)){
            return "无车";
        }
        return "无车";
    }

    public static String getHouse(String house) {
        if (StrUtil.equals(BaseTypeInfoEnum.House.hu001.getValue(), house)){
            return "无房";
        }else if (StrUtil.equals(BaseTypeInfoEnum.House.hu002.getValue(), house)){
            return "有房（红本在手）";
        }
        return "无房";
    }

    public static String getBaodan(String insurance) {
        if (StrUtil.equals(BaseTypeInfoEnum.Insurance.in001.getValue(), insurance)){
            return "无";
        }else if (StrUtil.equals(BaseTypeInfoEnum.Insurance.in002.getValue(), insurance)){
            return "投保两年以上";
        }
        return "无";
    }

    public static String getYuqi(String yuqi) {
        if (StrUtil.equals(BaseTypeInfoEnum.Yuqi.yq001.getValue(), yuqi)){
            return "逾期少于90天";
        }else if (StrUtil.equals(BaseTypeInfoEnum.Yuqi.yq002.getValue(), yuqi)){
            return "无逾期";
        }
        return "无逾期";
    }


    public static String getGjj(String gjj) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj001.getValue(), gjj)){
            return "无公积金";
        }else if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj002.getValue(), gjj)){
            return "6个月以上";
        }
        return "无公积金";
    }

    /**
     * 社保：6个月以上、6个月以下、无社保
     * @param socialInsurance
     * @return
     */
    public static String getShebao(String socialInsurance) {
        if (StrUtil.equals(BaseTypeInfoEnum.SocialInsurance.si001.getValue(), socialInsurance)){
            return "无社保";
        }else if (StrUtil.equals(BaseTypeInfoEnum.SocialInsurance.si002.getValue(), socialInsurance)){
            return "6个月以上";
        }
        return "无社保";
    }

    public static String getLoamAmount(String loanAmount) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la001.getValue(), loanAmount)){
            return "3万";
        }else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la002.getValue(), loanAmount)){
            return "5万";
        }else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la003.getValue(), loanAmount)){
            return "10万";
        }
        return "3万";
    }

    public static String getLoanTime(String loanTime) {
        return StrUtil.format("{}个月", loanTime);
    }
}